package com.tsd.core.excel.export;

import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Hillpool
 */
public class DefaultCellStyles implements CellStyles {

    protected Workbook wb;

    private final Map<ExcelStyle, CellStyle> styles;

    public enum ExcelStyle {
        HEADER_CELL("headerCell"), STRING_CELL("stringCell"), DEATE_CELL("dateCell"),
        NUMBER_CELL("numberCell"), TOTAL_CELL("totalCell"), BOLD_TITLE_CELL("boldTitleCell"),
        CURRENCY_CELL("currencyCell"), INTEGER_CELL("integerCell");

        private final String style;

        ExcelStyle(String style) {
            this.style = style;
        }

        public String getStyle() {
            return this.style;
        }
    }

    public enum ExcelFormat {
        DATE("yyyy-MM-dd"), DATETIME("yyyy-MM-dd HH:mm:ss"), NUMBER("0.00"), INTEGER("0"),
        CURRENCY("#,##0.00"), PERCENT("0.00%"), STRING("@");
        private final String pattern;

        ExcelFormat(String pattern) {
            this.pattern = pattern;
        }

        public String getPattern() {
            return this.pattern;
        }
    }

    public DefaultCellStyles(Workbook workbook) {
        this.wb = workbook;
        this.styles = createStyles();
    }

    @Override
    public void setWorkBook(Workbook workbook) {
        this.wb = workbook;
    }

    @Override
    public CellStyle getBoldTitleStyle() {
        return styles.get(ExcelStyle.BOLD_TITLE_CELL);
    }

    @Override
    public CellStyle getHeaderStyle() {
        return styles.get(ExcelStyle.HEADER_CELL);
    }

    @Override
    public CellStyle getStringStyle() {
        return styles.get(ExcelStyle.STRING_CELL);
    }

    @Override
    public CellStyle getDateStyle() {
        return styles.get(ExcelStyle.DEATE_CELL);
    }

    @Override
    public CellStyle getNumberStyle() {
        return styles.get(ExcelStyle.NUMBER_CELL);
    }

    @Override
    public CellStyle getCurrencyStyle() {
        return styles.get(ExcelStyle.CURRENCY_CELL);
    }

    @Override
    public CellStyle getIntegerStyle() {
        return styles.get(ExcelStyle.INTEGER_CELL);
    }

    @Override
    public CellStyle getFormulaStyle() {
        return styles.get(ExcelStyle.TOTAL_CELL);
    }

    protected Map<ExcelStyle, CellStyle> createStyles() {
        Map<ExcelStyle, CellStyle> styles = new HashMap<>();
        DataFormat df = wb.createDataFormat();

        // --字体设定 --//
        // 普通字体
        Font normalFont = wb.createFont();
        normalFont.setFontHeightInPoints((short) 10);
        normalFont.setFontName("宋体");

        // 加粗字体
        Font boldFont = wb.createFont();
        boldFont.setFontHeightInPoints((short) 10);
        boldFont.setBold(true);
        boldFont.setFontName("宋体");

        // 加粗标题字体
        Font boldTitleFont = wb.createFont();
        boldTitleFont.setFontHeightInPoints((short) 14);
        boldTitleFont.setBold(true);
        boldTitleFont.setFontName("宋体");

        // 合计字体
        Font totalFont = wb.createFont();
        totalFont.setFontHeightInPoints((short) 10);
        totalFont.setBold(true);
        totalFont.setFontName("宋体");

        // --Cell Style设定-- //
        // 加粗标题格式
        CellStyle boldTitleStyle = wb.createCellStyle();
        boldTitleStyle.setFont(boldTitleFont);
        boldTitleStyle.setAlignment(HorizontalAlignment.CENTER);
        boldTitleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        setBorder(boldTitleStyle);
        boldTitleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        boldTitleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        styles.put(ExcelStyle.BOLD_TITLE_CELL, boldTitleStyle);

        // 标题格式
        CellStyle headerStyle = wb.createCellStyle();
        headerStyle.setFont(normalFont);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        setBorder(headerStyle);
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        styles.put(ExcelStyle.HEADER_CELL, headerStyle);

        // String格式
        CellStyle stringStyle = wb.createCellStyle();
        stringStyle.setFont(normalFont);
        setBorder(stringStyle);
        stringStyle.setDataFormat(df.getFormat(ExcelFormat.STRING.getPattern()));
        styles.put(ExcelStyle.STRING_CELL, stringStyle);

        // 日期格式
        CellStyle dateCellStyle = wb.createCellStyle();
        dateCellStyle.setFont(normalFont);
        dateCellStyle.setDataFormat(df.getFormat(ExcelFormat.DATETIME.getPattern()));
        setBorder(dateCellStyle);
        styles.put(ExcelStyle.DEATE_CELL, dateCellStyle);

        // 数字格式
        CellStyle numberCellStyle = wb.createCellStyle();
        numberCellStyle.setFont(normalFont);
        numberCellStyle.setDataFormat(df.getFormat(ExcelFormat.NUMBER.getPattern()));
        setBorder(numberCellStyle);
        styles.put(ExcelStyle.NUMBER_CELL, numberCellStyle);

        // 金额格式
        CellStyle currencyCellStyle = wb.createCellStyle();
        currencyCellStyle.setFont(normalFont);
        currencyCellStyle.setDataFormat(df.getFormat(ExcelFormat.CURRENCY.getPattern()));
        setBorder(currencyCellStyle);
        styles.put(ExcelStyle.CURRENCY_CELL, currencyCellStyle);

        // 整数格式
        CellStyle integerCellStyle = wb.createCellStyle();
        integerCellStyle.setFont(normalFont);
        integerCellStyle.setDataFormat(df.getFormat(ExcelFormat.INTEGER.getPattern()));
        setBorder(integerCellStyle);
        styles.put(ExcelStyle.INTEGER_CELL, integerCellStyle);

        // 合计列格式
        CellStyle totalStyle = wb.createCellStyle();
        totalStyle.setFont(totalFont);
        totalStyle.setWrapText(true);
        totalStyle.setAlignment(HorizontalAlignment.CENTER);
        setBorder(totalStyle);
        styles.put(ExcelStyle.TOTAL_CELL, totalStyle);
        return styles;
    }

    protected void setBorder(CellStyle style) {
        // 设置边框
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());

        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());

        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());

        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());

        style.setAlignment(HorizontalAlignment.CENTER);//水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
    }
}
